Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
@esbuild-kit/cjs-loader
Advanced tools
Node.js loader for compiling ESM & TypeScript modules to CommonJS
@esbuild-kit/cjs-loader is an npm package that allows you to use ES modules (ESM) in CommonJS (CJS) environments. It leverages esbuild to transpile ESM to CJS on the fly, enabling seamless integration of modern JavaScript modules in older Node.js projects.
Transpile ESM to CJS
This feature allows you to transpile ES modules to CommonJS on the fly. By registering the loader, you can require ESM files in a CJS environment.
const { register } = require('@esbuild-kit/cjs-loader');
register();
const esmModule = require('./esmModule.mjs');
console.log(esmModule);
Custom Loader Options
You can customize the loader options such as the target ECMAScript version and the output format. This provides flexibility in how the ESM is transpiled to CJS.
const { register } = require('@esbuild-kit/cjs-loader');
register({ target: 'es2017', format: 'cjs' });
const esmModule = require('./esmModule.mjs');
console.log(esmModule);
The 'esm' package allows you to use ES modules in Node.js without the need for a build step. It provides a seamless way to load ESM in a CJS environment, similar to @esbuild-kit/cjs-loader, but without leveraging esbuild for transpilation.
The 'babel-register' package hooks into Node.js require to transpile files on the fly using Babel. It supports a wide range of JavaScript syntax transformations, including ESM to CJS, but may have a larger performance overhead compared to @esbuild-kit/cjs-loader.
The 'ts-node' package allows you to run TypeScript files directly in Node.js. It can also handle ESM to CJS transpilation when used with appropriate TypeScript configurations. It is more focused on TypeScript but can be used for similar purposes.
Node.js require()
hook for loading ESM & TypeScript.
.cjs
& .mjs
(.cts
& .mts
)node:
import prefixestsconfig.json
paths
Protip: use with esm-loader or tsx
cjs-loader only transforms CommonJS modules (
.cjs
/.cts
or.js
files incommonjs
type packages).To hook into
import()
calls or ES modules (.mjs
/.mts
extensions or.js
files inmodule
type packages), use this with esm-loader.Alternatively, use tsx to handle them both automatically.
npm install --save-dev @esbuild-kit/cjs-loader
Pass @esbuild-kit/cjs-loader
into the --require
flag
node -r @esbuild-kit/cjs-loader ./file.js
The following properties are used from tsconfig.json
in the working directory:
strict
: Whether to transform to strict modejsx
: Whether to transform JSX
Warning: When set to
preserve
, the JSX syntax will remain untransformed. To prevent Node.js from throwing a syntax error, chain another Node.js loader that can transform JSX to JS.
jsxFactory
: How to transform JSXjsxFragmentFactory
: How to transform JSX FragmentsjsxImportSource
: Where to import JSX functions fromallowJs
: Whether to apply the tsconfig to JS filespaths
: For resolving aliasestsconfig.json
pathBy default, tsconfig.json
will be detected from the current working directory.
To set a custom path, use the ESBK_TSCONFIG_PATH
environment variable:
ESBK_TSCONFIG_PATH=./path/to/tsconfig.custom.json node -r @esbuild-kit/cjs-loader ./file.js
Modules transformations are cached in the system cache directory (TMPDIR
). Transforms are cached by content hash so duplicate dependencies are not re-transformed.
Set environment variable ESBK_DISABLE_CACHE
to a truthy value to disable the cache:
ESBK_DISABLE_CACHE=1 node -r @esbuild-kit/cjs-loader ./file.js
tsx - Node.js runtime powered by esbuild using @esbuild-kit/cjs-loader
and @esbuild-kit/esm-loader
.
@esbuild-kit/esm-loader - TypeScript to ESM transpiler using the Node.js loader API.
FAQs
Node.js loader for compiling ESM & TypeScript modules to CommonJS
We found that @esbuild-kit/cjs-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.